package com.example.demo.controller;

import com.example.demo.entity.MenuEntity;
import com.example.demo.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @GetMapping(value = "/{id}")
    public MenuEntity getMenuById(@PathVariable int id) {
        return menuService.getMenuById(id);
    }

    @DeleteMapping(value = "/{id}")
    public int deleteMenuById(@PathVariable int id) {
        return menuService.deleteMenuById(id);
    }

    //    http://localhost:8081/user/list
    @GetMapping(value = "/list")
    public List<MenuEntity> list() {
        return menuService.list();
    }

    //  新增用户
    // POST http://localhost:8081/user
    @PostMapping
    public MenuEntity add(@RequestBody MenuEntity user) {
        int id = menuService.addMenu(user);
        return menuService.getMenuById(id);
    }

    @PutMapping
    public MenuEntity update(@RequestBody MenuEntity user) {

        // TODO 更新数据

        return menuService.getMenuById(user.getMenuId());
    }

}
